package com.tanhua.server.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.RecommendUser;
import com.tanhua.domain.vo.RecommendUserVo;
import com.tanhua.dubbo.api.RecommendUserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class RecommendUserService {
    @Reference
    private RecommendUserApi recommendUserApi;
    @Reference
    private UserInfoApi userInfoApi;

    /**
     * 推荐今日佳人
     *
     * @return
     */
    public RecommendUserVo RecommendUser() {
        //获取登入用户id
        Long userId = UserHolder.getUserId();
        //根据用户id查询推荐的今日佳人
        RecommendUser recommendUser = recommendUserApi.findRecommendUserId(userId);
        //非空判断，空则创建一个
        if (null==recommendUser){
            recommendUser = new RecommendUser();
           recommendUser.setUserId(userId%99+1);
           recommendUser.setScore(RandomUtils.nextDouble(76,87));
        }
        //根据佳人id查询佳人详情
        UserInfo userRecommend = userInfoApi.findById(recommendUser.getUserId());
        //构建vo
        RecommendUserVo recommendUserVo = new RecommendUserVo();
        BeanUtils.copyProperties(userRecommend,recommendUserVo);
        recommendUserVo.setTags(StringUtils.split(userRecommend.getTags(),","));
        recommendUserVo.setFateValue(recommendUser.getScore().longValue());
        //返回对象
        return recommendUserVo;
    }
}
